<?php
class packing_list_packages_cargos extends ActiveRecord {
	/**
	 *
	 * The followings are the available columns in table 'packing_list_packages':
	 * @var integer $id
	 * @var integer $package_id
	 * @var integer $pkg
	 * @var integer $cargo_id
	 * @var integer $date
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class	 */
	public $ref_no;
	public static function model($className=__CLASS__) {
		return parent::model($className);
	}
	
	/* 
	@return string the associated database table name	 
	*/
	
	public function tableName() {
		return 'packing_list_packages_cargos';
	}
	
	/*
	 @return array validation rules for model attributes.	 
	 */
	
	public function rules() {
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('ref_no','safe'),
			array('cargo_id, package_id, date, pkg', 'required'),
			array('cargo_id','checkCargo'),
			array('cargo_id, package_id, id, date, pkg, wt, wt_type', 'numerical', 'integerOnly'=>true),
		);
	}
	public function afterFind(){
		$db = $this->dbConnection;
        $criteria = new CDbCriteria;
        $criteria->condition = "id=:cargo_id";
        $criteria->params = array(':cargo_id'=>$cargo_id);
        $result = $db->commandBuilder->createFindCommand('cargo',$criteria)->query();
        $row = $result->read();
        $this->ref_no = $row['ref_no'];
        return parent::afterFind();
	}	
	/*
	@return array relational rules.	 
	*/
	public function checkCargo($attr,$params) {
		$cargo = cargo::model()->findByPk($this->$attr);
		if($cargo === null || $this->$attr == 0)
			return false;
		$cargo->setScenario("packing_list");
        if(!$cargo->validate())
        	$this->addError($attr,'Error in cargo information. Please check you have all required information in cargo.');
	}
	public function relations() {
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
        	'cargo_info'=>array(self::BELONGS_TO,'cargo','cargo_id'),
        	'package'=>array(self::BELONGS_TO,'packing_list_packages','package_id'),
		);
	}
	public function beforeValidate(){
		$this->date = time();
		return parent::beforeValidate();
	}
	public function afterValidate() {
		$this->wt = $this->getLeftOverWt();
		return parent::afterValidate();
	}
	
	/* 
	 * @return array customized attribute labels (name=>label)	 
	 */
	public function getLeftOverWt() {
		$cargo = cargo::model()->findByPk($this->cargo_id);
		if($this->cargo_id <=0 || $cargo === null)
			return 0;
		return $cargo->getLeftOverWt();
	}
	public function getAvailableQty() {
		if($this->isNewRecord)
            return array(0);
        return $this->cargo_info()->getAvailableQty($this->pkg);		
	}
	public function getTotalQty(){
		if($this->isNewRecord)
            return 0;
		return $this->cargo_info()->getTotalQty();
	}
	public function attributeLabels() {
		return array(
            'ref_no'=>'Ref No.'
		);
	}
}
